package models

import "time"

type HealthGuides struct {
	GuideID         uint64    `gorm:"column:guide_id;primary_key;auto_increment" json:"guide_id"`
	Title           string    `gorm:"column:title;not null" json:"title" `
	Description     string    `gorm:"column:description;not null" json:"description"`
	UserID          uint64    `gorm:"column:user_id;not null" json:"user_id" `
	NutritionalInfo string    `gorm:"column:nutritional_info;not null" json:"nutritional_info" `
	TargetAudience  string    `gorm:"column:target_audience;not null" json:"target_audience"`
	CreatedAt       time.Time `gorm:"column:create_at;autoCreateTime" json:"create_at"`
}

func (guide *HealthGuides) Delete() error {
	return DB.Self.Delete(&guide).Error
}
func (guide *HealthGuides) Create() error {
	return DB.Self.Create(&guide).Error
}

func (guide *HealthGuides) Update() error {
	return DB.Self.Model(&guide).Update(&guide).Error
}

// 查询，提供Id查询
func GetHealthGuidesById(id uint64) (*HealthGuides, error) {
	guide := &HealthGuides{}
	r := DB.Self.First(guide, id)
	return guide, r.Error
}

// 查询个人健康饮食
func GetGuideByUserId(userId uint64) ([]*HealthGuides, error) {
	guide := make([]*HealthGuides, 0)
	err := DB.Self.Where("user_id = ?", userId).Find(&guide).Error
	return guide, err
}

// 查询所有健康饮食
func GetGuide() ([]*HealthGuides, error) {
	guide := make([]*HealthGuides, 0)
	err := DB.Self.Find(&guide).Error
	return guide, err
}

// 模糊查询健康饮食
func GetGuideListByLike(key string) ([]*HealthGuides, error) {
	guideList := make([]*HealthGuides, 0)
	err := DB.Self.Where("title like ? ", "%"+key+"%").Find(&guideList).Error
	return guideList, err
}
